package view.ui
{
	import control.manager.LayerManager;
	import control.manager.RoleManager;
	import control.manager.TaskManager;
	
	import data.GameConst;
	import data.PlayerData;
	import data.TaskVO;
	import data.UIVO;
	import data.UserTaskVO;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import view.item.AcceptTaskEffect;
	import view.item.CompleteTaskEffect;
	import view.layer.GameLayer;
	import view.role.MapMonster;
	import view.role.MapNPC;
	import view.role.MapObject;
	import view.role.MapPlayer;
	

	/**
	 * 任务谈话UI 
	 * @author leo
	 * 
	 */	
	public class UITaskTalk extends UILoadingBase
	{
		private const ACCEPT_TASK :int = 1;
		private const COMPLETE_TASK :int = 2;
		
		
		public var btnOKContainer :MovieClip;
		public var mcTaskAward :MovieClip;
		public var mcTaskListContainer :MovieClip;
		/**
		 * npc头像容器 
		 */		
		public var npcHeader :MovieClip;
		
		/**
		 * 关闭按钮 
		 */		
		public var btnClose :SimpleButton;
		/**
		 * 任务描述文本 
		 */		
		public var txtMemo :TextField;
		/**
		 * NPC名字文本 
		 */		
		public var txtNPCName :TextField;
		/**
		 * 奖励文本 
		 */		
		public var txtReward :TextField;
		/**
		 * 确定按钮上显示的文本 
		 */		
		public var txtOK :TextField;
		/**
		 * 确定 接受按钮 
		 */		
		public var btnResponse :SimpleButton;
		
		/**
		 * 当前窗口引用的NPC 
		 */		
		private var npc :MapNPC;
		/**
		 * 可接任务列表，根据玩家的数据来显示 
		 */		
		private var canAcceptTaskList :Array;
		
		/**
		 * 是否第一次点击ok按钮，谈话中第一次是NPC的废话对白，第二次才是任务的详细描述 
		 */		
		private var theFirstClick :Boolean = true;
		/**
		 * 当前弹窗状态（接任务弹窗还是完成任务弹窗） 
		 */		
		private var curStatus :int = 1;
		public function UITaskTalk(npc :MapNPC = null,isFirstGame :Boolean = false)
		{
			this.npc = npc;
			if(isFirstGame)
			{
				canAcceptTaskList = npc.npcData.canDoList;
			}else
			{
				canAcceptTaskList = TaskManager.instance.curNPCCanDoList(npc);
			}
			var vo :UIVO = new UIVO("taskTalk","NPCTaskWindow");
			super(vo);
		}
		
		override protected function swfLoadComplete():void
		{
			super.swfLoadComplete();
			
			if(!content)
			{
				return;
			}
			this.btnOKContainer = content.btnOKContainer;
			this.mcTaskAward = content.mcTaskAward;
			this.mcTaskAward.visible = false;
			this.mcTaskListContainer = content.mcTaskListContainer;
			this.npcHeader = content.npcHeader;
			
			this.btnClose = this.content.btnClose;
			this.txtReward = this.mcTaskAward.txtReward;
			this.txtOK = this.btnOKContainer.txtOK;
			this.txtNPCName = this.content.txtNPCName;
			this.btnResponse = this.btnOKContainer.btnResponse;
			this.txtMemo = this.content.txtMemo;
			
			 if(this.npc)
			 {
				this.txtNPCName.text = this.npc.npcData.roleName;
				this.txtMemo.text = this.npc.npcData.npcSpeak;
			 }
			 
			 
			 var txtFormat :TextFormat = new TextFormat();
			 txtFormat.underline = true;
			 this.txtOK.defaultTextFormat = txtFormat;
			 if(canAcceptTaskList &&this.canAcceptTaskList.length > 0)  //有可接任务
			 {
				 this.curStatus = ACCEPT_TASK;
				 this.data = this.canAcceptTaskList[0];
				 this.txtOK.text = TaskManager.instance.getStringByTaskVO(this.taskVO);
			 }else   //无可接任务
			 {
				this.txtOK.text = "再见！！";
			 }
			 
			 
			 if(playerData.taskingList)
			 {
				 for each(var userTaskVO :UserTaskVO in playerData.taskingList)
				 {
					 if(userTaskVO.canComplete && userTaskVO.completeID == npc.npcData.npcID)
					 {
						 this.curStatus = COMPLETE_TASK;
						 theFirstClick = false;
						 this.txtOK.text = TaskManager.instance.getStringByTaskVO(userTaskVO.taskVO,TaskVO.IS_COMPLETE,true,true);
						 this.data = userTaskVO;
						 //显示奖励
						 this.mcTaskAward.visible = true;
						 this.txtReward.text = "经验： " + userTaskVO.taskVO.rewardExp + "\n" + "金币： " + userTaskVO.taskVO.rewardGold;
						 this.txtMemo.text = userTaskVO.taskVO.completeSpeak;
						 break;
					 }
				 }
			 }
			
			addEventListener(MouseEvent.MOUSE_UP,mouseHandler);
			this.btnOKContainer.addEventListener(MouseEvent.MOUSE_OVER,overOutHandler);
			this.btnOKContainer.addEventListener(MouseEvent.MOUSE_OUT,overOutHandler);
		}
		
		private function mouseHandler(e :MouseEvent):void
		{
			var npc :MapNPC
			switch(e.target)
			{
				//关闭
				case this.btnClose:
					destroy();
					break;
				//接受
				case this.btnResponse:
					trace("ok");
					if(this.curStatus == this.ACCEPT_TASK)  //接取任务
					{
						if(!this.taskVO)
						{
							destroy();
							return;
						}
						if(theFirstClick)  //切换文字
						{
							this.txtMemo.text = this.taskVO.taskMemo;
							this.txtOK.text = this.taskVO.receiveTaskSpeak;
							theFirstClick = false;
							//显示奖励
							this.mcTaskAward.visible = true;
							this.txtReward.text = "经验： " + taskVO.rewardExp + "\n" + "金币： " + taskVO.rewardGold;
						}else
						{
							if(playerData.canDo(taskVO))
							{
								playerData.addTasking(taskVO);
								RoleManager.instance.changeNpcStatus(playerData);
								var effect :AcceptTaskEffect = new AcceptTaskEffect();
								effect.x = player.position.x;
								effect.y = player.position.y;
								gameLayer.roleLayer.addChild(effect);
								
								UITaskTrack.I.removeTask(taskVO);
								UITaskTrack.I.acceptTask();
								
								if(taskVO.taskSort == TaskVO.TALK_SORT)  //谈话任务
								{
									npc = RoleManager.instance.findNPCByID(taskVO.completeNPCID);
									if(npc)
									{
										player.onClick(npc);
									}else
									{
										//如果没有找到NPC，检查下是否需要是其他地图的NPC
										RoleManager.instance.isNeedChangeMapByNPC(taskVO.completeNPCID);
									}
								}else if(taskVO.taskSort == TaskVO.BATTLE_SORT)   //战斗任务
								{
									if(taskVO.battleTaskData.length > 0)
									{
										var monsterID :String = taskVO.battleTaskData[1];
										var monster :MapMonster = RoleManager.instance.findMonsterByID(monsterID);
										if(monster)
										{
											player.onClick(monster);
										}else
										{
											RoleManager.instance.isNeedChangeMapByMonster(monsterID);
										}
									}
								}
								
								destroy();
							}
						}
					}else if(this.curStatus == this.COMPLETE_TASK)   //完成任务
					{
						
						userTaskVO.canComplete = false;
						userTaskVO.isComplete = true;
						userTaskVO.isTasking = false;
						RoleManager.instance.changeNpcStatus(playerData);
						playerData.addCompleteTask(userTaskVO);
						playerData.updateAssetsByTask(userTaskVO);
						//完成任务效果
						var comEffect :CompleteTaskEffect = new CompleteTaskEffect();
						comEffect.x = player.position.x;
						comEffect.y = player.position.y;
						gameLayer.roleLayer.addChild(comEffect);
						//删除完成的任务
						UITaskTrack.I.removeTask(userTaskVO.taskVO);
						UITaskTrack.I.completeTask();
						
						var list :Array = TaskManager.instance.curNPCCanDoList(this.npc);
						if(list.length > 0)//该NPC有后续任务
						{
							UITaskTrack.I.addTask(list[0]);
							var ui :UITaskTalk = new UITaskTalk(this.npc);
							this.npc.setTaskStatus(GameConst.HAVE_TASK);
							UIPopupLayer.I.isShow = false;
							UIPopupLayer.I.show(ui);
						}else  //没有后续任务，但有一个任务可以做
						{
							if(userTaskVO.netxTaskID)
							{
								var vo :TaskVO = TaskManager.instance.findTaskByID(userTaskVO.netxTaskID);
								if(vo)
								{
									npc = null;
									var canDO :Boolean = playerData.canDo(vo);
									if(canDO)
									{
										UITaskTrack.I.addTask(vo);
										npc = RoleManager.instance.findNPCByID(vo.sendNPCID);
										if(npc)
										{
											npc.setTaskStatus(GameConst.HAVE_TASK);
										}else
										{
											//如果没有找到NPC，检查下是否需要是其他地图的NPC
											RoleManager.instance.isNeedChangeMapByNPC(vo.sendNPCID);
										}
										npc.onClick(npc);
									}
								}
							}
						}
						destroy();
					}
					break;
			}
		}
		
		private function overOutHandler(e :MouseEvent):void
		{
			switch(e.type)
			{
				case MouseEvent.MOUSE_OVER:
					this.btnOKContainer.filters = MapObject.MOUSE_ON;
					break;
				case MouseEvent.MOUSE_OUT:
					this.btnOKContainer.filters = null;
					break;
			}
		}
		
		override public function destroy():void
		{
			if(this.parent)
			{
				var par :UIPopupLayer = this.parent as UIPopupLayer;
				if(par)
				{
					par.visible = false;
					par.isShow = false;
					par.removeChild(this);
				}else
				{
					return;
				}
			}
			removeEventListener(MouseEvent.MOUSE_UP,mouseHandler);
			if(btnOKContainer)
			{
				this.btnOKContainer.removeEventListener(MouseEvent.MOUSE_OVER,overOutHandler);
				this.btnOKContainer.removeEventListener(MouseEvent.MOUSE_OUT,overOutHandler);
			}
			this.npc = null;
			this.canAcceptTaskList = null;
		}
		
		private function get taskVO ():TaskVO
		{
			return data as TaskVO;
		}
		
		private function get playerData():PlayerData
		{
			return RoleManager.instance.playerData;
		}
		
		private function get player():MapPlayer
		{
			var gameLayer :GameLayer = LayerManager.instance.getLayer(GameLayer.NAME) as GameLayer;
			
			return gameLayer.player;
		}
		
		private function get gameLayer():GameLayer
		{
			return LayerManager.instance.getLayer(GameLayer.NAME) as GameLayer;
		}
		
		private function get userTaskVO():UserTaskVO
		{
			return data as UserTaskVO;
		}
	}
}